//
//  UserChangeViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 16/1/21.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "UserChangeViewController.h"
#import "JMJLoginTextField.h"

#define kViewTopMargin 55 / 2
#define kCellHeight 85 / 2
#define kLabelLeftMargin 30 / 2
#define kLabelRightMargin 30 / 2
#define kLabelWidth 170
#define kTextFieldTopMargin 15 / 2
#define kButtonTopMargin 150 / 2
#define kButtonHeight 80 / 2
#define kTextRightMargin 25 / 2

#define kBigFontSize 14
#define kSmallFontSize 12

@interface UserChangeViewController () <UITextFieldDelegate>

@end

@implementation UserChangeViewController

{
    JMJNormalTextField *_changeTextField;
    UIView *_backView;
    UILabel *_buttonLabel;
    UIView *_verifyView;
    UIView *_changePasswordView;
    JMJNormalTextField *_phoneNumberTextField;
    JMJNormalTextField *_verifyNumberTextField;
    JMJNormalTextField *_changePasswordTextField1;
    JMJNormalTextField *_changePasswordTextField2;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNav];
    [self customMainViewWithTitle:self.title];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

#pragma mark - Custom User Interface

- (void)customNav{
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(bbiClick:)];
    self.navigationItem.leftBarButtonItem = bbi;
    NSLog(@"%@", self.originDetail);
}

- (void)customMainViewWithTitle:(NSString *)title{
    if ([title isEqualToString:@"修改登录密码"]) {
        [self customVerifyView];
    }else{
        [self customMainView];
    }
}

- (void)customMainView{
    
    self.view.backgroundColor = RGB_COLOR(@"#f6f6f6");
    
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, kViewTopMargin + kNavigationBarHeight, UISCREENWIDTH, 2 * kCellHeight)];
    _backView.backgroundColor = [UIColor whiteColor];
    
    UILabel *originLabel = [[UILabel alloc] initWithFrame:CGRectMake(kLabelLeftMargin, 0, kLabelWidth, kCellHeight)];
    originLabel.textAlignment = NSTextAlignmentLeft;
    originLabel.font = [UIFont systemFontOfSize:kBigFontSize];
    NSString *titleString = self.title;
    NSString *newString = [titleString stringByReplacingOccurrencesOfString:@"修改" withString:@""];
    originLabel.text = [NSString stringWithFormat:@"原%@：", newString];
    CGRect originRect = [originLabel textRectForBounds:originLabel.bounds limitedToNumberOfLines:1];
    originLabel.frame = CGRectMake(kLabelLeftMargin, 0, originRect.size.width, kCellHeight);
    
    UILabel *originDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(originLabel.right + kLabelRightMargin, 0, kLabelWidth, kCellHeight)];
    originDetailLabel.textAlignment = NSTextAlignmentLeft;
    originDetailLabel.font = [UIFont systemFontOfSize:kSmallFontSize];
    originDetailLabel.textColor = RGB_COLOR(@"#c9c9c9");
    originDetailLabel.text = self.originDetail;
    
    UIView *separateView = [[UIView alloc] initWithFrame:CGRectMake(kLabelLeftMargin, kCellHeight, UISCREENWIDTH - 2 * kLabelLeftMargin, 1)];
    separateView.backgroundColor = RGB_COLOR(@"#c9c9c9");
    
    UILabel *changeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kLabelLeftMargin, kCellHeight, originLabel.width, kCellHeight)];
    changeLabel.textAlignment = NSTextAlignmentLeft;
    changeLabel.font = [UIFont systemFontOfSize:kBigFontSize];
    changeLabel.text = [NSString stringWithFormat:@"新%@：", newString];
    
    _changeTextField = [[JMJNormalTextField alloc] initWithFrame:CGRectMake(changeLabel.right + kLabelRightMargin, kCellHeight + kTextFieldTopMargin, UISCREENWIDTH - kLabelLeftMargin * 2 - kLabelRightMargin - changeLabel.width, kCellHeight - 2 * kTextFieldTopMargin)];
    _changeTextField.layer.borderColor = [RGB_COLOR(@"#c9c9c9") CGColor];
    _changeTextField.layer.borderWidth = 2;
    _changeTextField.layer.cornerRadius = 3;
    _changeTextField.delegate = self;
    
    if ([newString hasPrefix:@"手机"]) {
        _changeTextField.keyboardType = UIKeyboardTypeNumberPad;
    }else{
        _changeTextField.keyboardType = UIKeyboardTypeASCIICapable;
    }
    
    [_backView addSubview:originLabel];
    [_backView addSubview:originDetailLabel];
    [_backView addSubview:separateView];
    [_backView addSubview:changeLabel];
    [_backView addSubview:_changeTextField];
    
    [self.view addSubview:_backView];
    
    _buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(kLabelLeftMargin, _backView.bottom + kButtonTopMargin, separateView.width, kButtonHeight)];
    _buttonLabel.text = @"保存";
    _buttonLabel.backgroundColor = RGB_COLOR(@"#d91f00");
    _buttonLabel.textColor = [UIColor whiteColor];
    _buttonLabel.textAlignment = NSTextAlignmentCenter;
    _buttonLabel.layer.cornerRadius = 5;
    _buttonLabel.clipsToBounds = YES;
    _buttonLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeValue)];
    [_buttonLabel addGestureRecognizer:tap];
    [self.view addSubview:_buttonLabel];
    
}

- (void)customVerifyView{
    
    self.view.backgroundColor = RGB_COLOR(@"#f6f6f6");
    
    _verifyView = [[UIView alloc] initWithFrame:CGRectMake(0, kViewTopMargin + kNavigationBarHeight, UISCREENWIDTH, 2 * kCellHeight)];
    _verifyView.backgroundColor = [UIColor whiteColor];
    
    UILabel *phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(kLabelLeftMargin, 0, 100, kCellHeight)];
    phoneNumberLabel.text = @"手机号码：";
    phoneNumberLabel.font = [UIFont systemFontOfSize:kBigFontSize];
    phoneNumberLabel.textAlignment = NSTextAlignmentLeft;
    CGRect rect = [phoneNumberLabel textRectForBounds:phoneNumberLabel.bounds limitedToNumberOfLines:1];
    phoneNumberLabel.frame = CGRectMake(kLabelLeftMargin, 0, rect.size.width, kCellHeight);
    _phoneNumberTextField = [[JMJNormalTextField alloc] initWithFrame:CGRectMake(phoneNumberLabel.right + kLabelLeftMargin, kTextFieldTopMargin, UISCREENWIDTH - 2 * kLabelLeftMargin - rect.size.width - kLabelRightMargin, kCellHeight - 2 * kTextFieldTopMargin)];
    _phoneNumberTextField.layer.borderWidth = 2;
    _phoneNumberTextField.layer.borderColor = [RGB_COLOR(@"#c9c9c9") CGColor];
    _phoneNumberTextField.layer.cornerRadius = 3;
    _phoneNumberTextField.delegate = self;
    _phoneNumberTextField.keyboardType = UIKeyboardTypeNumberPad;
    _phoneNumberTextField.tag = 1;
    UIView *separateView = [[UIView alloc] initWithFrame:CGRectMake(kLabelLeftMargin, phoneNumberLabel.bottom, UISCREENWIDTH - 2 * kLabelLeftMargin, 1)];
    separateView.backgroundColor = RGB_COLOR(@"#c9c9c9");
    UILabel *verifyLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneNumberLabel.left, phoneNumberLabel.bottom, phoneNumberLabel.width, kCellHeight)];
    verifyLabel.text = @"验证码：";
    verifyLabel.textAlignment = NSTextAlignmentLeft;
    verifyLabel.font = [UIFont systemFontOfSize:kBigFontSize];
    _verifyNumberTextField = [[JMJNormalTextField alloc] initWithFrame:CGRectMake(_phoneNumberTextField.left, kCellHeight + kTextFieldTopMargin, (_phoneNumberTextField.width - kTextRightMargin) / 2, kCellHeight - 2 * kTextFieldTopMargin)];
    _verifyNumberTextField.layer.borderColor = [RGB_COLOR(@"#c9c9c9") CGColor];
    _verifyNumberTextField.layer.borderWidth = 2;
    _verifyNumberTextField.layer.cornerRadius = 3;
    _verifyNumberTextField.delegate = self;
    _verifyNumberTextField.tag = 2;
    _verifyNumberTextField.keyboardType = UIKeyboardTypeASCIICapable;
    UILabel *getVerifyNumber = [[UILabel alloc] initWithFrame:CGRectMake(_verifyNumberTextField.right + kTextRightMargin, kCellHeight + kTextFieldTopMargin, _verifyNumberTextField.width, kCellHeight - 2 * kTextFieldTopMargin)];
    getVerifyNumber.text = @"获取验证码";
    getVerifyNumber.textAlignment = NSTextAlignmentCenter;
    getVerifyNumber.backgroundColor = RGB_COLOR(@"#d91f00");
    getVerifyNumber.layer.cornerRadius = 5;
    getVerifyNumber.clipsToBounds = YES;
    getVerifyNumber.userInteractionEnabled = YES;
    getVerifyNumber.textColor = [UIColor whiteColor];
    getVerifyNumber.font = [UIFont systemFontOfSize:kBigFontSize];
    UITapGestureRecognizer *getVerifyTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getVerifyNumber)];
    [getVerifyNumber addGestureRecognizer:getVerifyTap];
    
    [_verifyView addSubview:phoneNumberLabel];
    [_verifyView addSubview:_phoneNumberTextField];
    [_verifyView addSubview:separateView];
    [_verifyView addSubview:verifyLabel];
    [_verifyView addSubview:_verifyNumberTextField];
    [_verifyView addSubview:getVerifyNumber];
    
    _buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(kLabelLeftMargin, _verifyView.bottom + kButtonTopMargin, separateView.width, kButtonHeight)];
    _buttonLabel.text = @"下一步，重置密码";
    _buttonLabel.backgroundColor = RGB_COLOR(@"#d91f00");
    _buttonLabel.textColor = [UIColor whiteColor];
    _buttonLabel.textAlignment = NSTextAlignmentCenter;
    _buttonLabel.layer.cornerRadius = 5;
    _buttonLabel.clipsToBounds = YES;
    _buttonLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(verifiedToChange)];
    [_buttonLabel addGestureRecognizer:tap];
    [self.view addSubview:_buttonLabel];
    
    [self.view addSubview:_verifyView];
}

- (void)customChangePasswordView{
    [_verifyView removeFromSuperview];
    [_buttonLabel removeFromSuperview];
    
    self.title = @"重置密码";
    
    _changePasswordView = [[UIView alloc] initWithFrame:CGRectMake(0, kViewTopMargin + kNavigationBarHeight, UISCREENWIDTH, 2 * kCellHeight)];
    _changePasswordView.backgroundColor = [UIColor whiteColor];
    
    UILabel *originLabel = [[UILabel alloc] initWithFrame:CGRectMake(kLabelLeftMargin, 0, kLabelWidth, kCellHeight)];
    originLabel.textAlignment = NSTextAlignmentLeft;
    originLabel.font = [UIFont systemFontOfSize:kBigFontSize];
    originLabel.text = @"设置密码";
    CGRect originRect = [originLabel textRectForBounds:originLabel.bounds limitedToNumberOfLines:1];
    originLabel.frame = CGRectMake(kLabelLeftMargin, 0, originRect.size.width, kCellHeight);
    
    _changePasswordTextField1 = [[JMJNormalTextField alloc] initWithFrame:CGRectMake(originLabel.right + kLabelRightMargin, kTextFieldTopMargin, UISCREENWIDTH - kLabelLeftMargin * 2 - kLabelRightMargin - originLabel.width, kCellHeight - 2 * kTextFieldTopMargin)];
    _changePasswordTextField1.layer.borderColor = [RGB_COLOR(@"#c9c9c9") CGColor];
    _changePasswordTextField1.layer.borderWidth = 2;
    _changePasswordTextField1.layer.cornerRadius = 3;
    _changePasswordTextField1.keyboardType = UIKeyboardTypeASCIICapable;
    _changePasswordTextField1.delegate = self;
    _changePasswordTextField1.tag = 3;
    
    UIView *separateView = [[UIView alloc] initWithFrame:CGRectMake(kLabelLeftMargin, kCellHeight, UISCREENWIDTH - 2 * kLabelLeftMargin, 1)];
    separateView.backgroundColor = RGB_COLOR(@"#c9c9c9");
    
    UILabel *changeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kLabelLeftMargin, kCellHeight, originLabel.width, kCellHeight)];
    changeLabel.textAlignment = NSTextAlignmentLeft;
    changeLabel.font = [UIFont systemFontOfSize:kBigFontSize];
    changeLabel.text = @"重复密码";
    
    _changePasswordTextField2 = [[JMJNormalTextField alloc] initWithFrame:CGRectMake(changeLabel.right + kLabelRightMargin, kCellHeight + kTextFieldTopMargin, UISCREENWIDTH - kLabelLeftMargin * 2 - kLabelRightMargin - changeLabel.width, kCellHeight - 2 * kTextFieldTopMargin)];
    _changePasswordTextField2.layer.borderColor = [RGB_COLOR(@"#c9c9c9") CGColor];
    _changePasswordTextField2.layer.borderWidth = 2;
    _changePasswordTextField2.layer.cornerRadius = 3;
    _changePasswordTextField2.delegate = self;

    [_changePasswordView addSubview:originLabel];
    [_changePasswordView addSubview:_changePasswordTextField1];
    [_changePasswordView addSubview:separateView];
    [_changePasswordView addSubview:changeLabel];
    [_changePasswordView addSubview:_changePasswordTextField2];
    
    [self.view addSubview:_changePasswordView];
    
    _buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(kLabelLeftMargin, _changePasswordView.bottom + kButtonTopMargin, separateView.width, kButtonHeight)];
    _buttonLabel.text = @"确定";
    _buttonLabel.backgroundColor = RGB_COLOR(@"#d91f00");
    _buttonLabel.textColor = [UIColor whiteColor];
    _buttonLabel.textAlignment = NSTextAlignmentCenter;
    _buttonLabel.layer.cornerRadius = 5;
    _buttonLabel.clipsToBounds = YES;
    _buttonLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changePassword)];
    [_buttonLabel addGestureRecognizer:tap];
    [self.view addSubview:_buttonLabel];
}

#pragma mark - Action

- (void)bbiClick:(UIBarButtonItem *)bbi{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)changeValue{
    self.changeDetailBlock(_changeTextField.text);
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)getVerifyNumber{
    
}

- (void)verifiedToChange{
    [self customChangePasswordView];
}

- (void)changePassword{
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark - TextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
